package com.example.googlemarketdemo;

import holder.MenuHolder;

import com.astuetz.PagerSlidingTabStripExtends;

import utils.UIUtils;

import base.BaseFragment;
import base.LoadingPager;



import factory.FragmentFactory;

import activity.BaseActivity;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.FrameLayout;

public class MainActivity extends BaseActivity {

	PagerSlidingTabStripExtends mtabs;
	ViewPager mViewPager;
	String[] maintitles;

/*	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
		initData();
		initListener();

	}*/

	/**
	 * 手动触发加载数据
	 */
	@Override
	protected void initListener() {
		mtabs.setOnPageChangeListener(myOnPageChangeListener);

		mViewPager.getViewTreeObserver().addOnGlobalLayoutListener(
				new OnGlobalLayoutListener() {

					@Override
					public void onGlobalLayout() {
						// 手动触发onPageSelected方法
						myOnPageChangeListener.onPageSelected(0);
						// 只监听一次
						mViewPager.getViewTreeObserver()
								.removeGlobalOnLayoutListener(this);
					}
				});

	}

	MyOnPageChangeListener myOnPageChangeListener = new MyOnPageChangeListener();
	private ActionBar actionBar;
	private ActionBarDrawerToggle toggle;
	private FrameLayout main_fl_menu;

	class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {

		}

		@Override
		public void onPageSelected(int position) {
			BaseFragment baseFragment = FragmentFactory.creatFragment(position);

			if (baseFragment != null) {
				LoadingPager loadingPager = baseFragment.getLoadingpager();
				if (loadingPager != null) {
					// 触发加载数据
					loadingPager.tiggerloadData();
				}
			}

		}

		@Override
		public void onPageScrollStateChanged(int state) {
			// TODO Auto-generated method stub

		}

	}

	@Override
	protected void initData() {
		// 获取页签的数据

		maintitles = UIUtils.getStringArr(R.array.main_titles);

		// 设置Adapter,
		mViewPager.setAdapter(new MainFragmentPagerAdapter(
				getSupportFragmentManager()));

		// 绑定页签
		mtabs.setViewPager(mViewPager);
	}

	// 自定义一个类继承Fragment 的adapter FragmentStatePagerAdapter
	class MainFragmentPagerAdapter extends FragmentPagerAdapter {

		public MainFragmentPagerAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int arg0) {
			Fragment fragment = FragmentFactory.creatFragment(arg0);
			return fragment;
		}

		@Override
		public int getCount() {
			if (maintitles != null) {
				return maintitles.length;
			}
			return 0;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			// TODO Auto-generated method stub
			return maintitles[position];
		}

	}

	@Override
	protected void initView() {
		// 设置现实的xml文件
		setContentView(R.layout.activity_main);

		// 获取控件
		// 页签
		mtabs = (PagerSlidingTabStripExtends) findViewById(R.id.pstab);
		// ViewPager
		mViewPager = (ViewPager) findViewById(R.id.main_viewpager);

		//创建menu对象
		MenuHolder menuHolder = new MenuHolder();
		//获取ActionBar的左侧菜单
		main_fl_menu = (FrameLayout) findViewById(R.id.main_fl_menu);
		main_fl_menu.addView(menuHolder.mHolderView);
		
		menuHolder.setDataAndRefreshHolderView(null);
		
		//获取ActionBar
		initActionBar();
		
	}

	private void initActionBar() {
		//获取了ActionBar
		actionBar = getSupportActionBar();
		actionBar.setTitle("GooglePlay");
		//显示回退部分
		actionBar.setDisplayHomeAsUpEnabled(true);
		
		DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.main_drawerLayout);
		
		//处理
		toggle = new ActionBarDrawerToggle(
				this,
				drawerLayout,
				R.drawable.ic_drawer_am,
				R.string.open,
				R.string.close);
		
		//同步方法
		toggle.syncState();
		
		//设置监听
		drawerLayout.setDrawerListener(toggle);
	}

	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			toggle.onOptionsItemSelected(item);
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
}
